package com.carpark.ejb.DAO;

import com.carpark.ejb.Model.TarrifsModel;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
 *
 * @author Sawior
 */
public class TarrifsDAOImpl implements TarrifsDAO {

    private EntityManager entityManager;

    public TarrifsDAOImpl() {
        EntityManagerFactory factory = Persistence.createEntityManagerFactory("persistenceUnit");
        entityManager = factory.createEntityManager();
    }

    @Override
    public void addTarrif(TarrifsModel tarrif) {
        entityManager.getTransaction().begin();
        entityManager.persist(tarrif);
        entityManager.flush();
        entityManager.refresh(tarrif);
        entityManager.getTransaction().commit();
    }

    @Override
    public void updateTarrif(TarrifsModel tarrif) {
        try{
        TarrifsModel actual = getTarrifsById(tarrif.getTarrifId());
        entityManager.getTransaction().begin();
        entityManager.persist(actual);
        
        actual.setName(tarrif.getName());
        actual.setCarType(tarrif.getCarType());
        actual.setComments(tarrif.getComments());
        actual.setDurationTime(tarrif.getDurationTime());
        actual.setEndDate(tarrif.getEndDate());
        actual.setIssueDate(tarrif.getIssueDate());
        actual.setRank(tarrif.getRank());
        
        entityManager.getTransaction().commit();
        }
        catch(Throwable t){
            updateTarrif(tarrif);
        }
    }

    @Override
    public void removeTarrif(TarrifsModel tarrif) {
     TarrifsModel rowToDelete = getTarrifsById(tarrif.getTarrifId());
        entityManager.getTransaction().begin();
        entityManager.remove(rowToDelete);
        entityManager.getTransaction().commit();
    }

    @Override
    public List<TarrifsModel> getAllTarrifs() {
        return (List<TarrifsModel>) entityManager.createNamedQuery("TarrifsModel.findAll").getResultList();
    }

    @Override
    public int getTarrifsSize() {
        return entityManager.createNamedQuery("TarrifsModel.findAll").getResultList().size();
    }

    @Override
    public TarrifsModel getTarrifsById(Integer id) {
        return (TarrifsModel) entityManager.createNamedQuery("TarrifsModel.findByTarrifId").setParameter("tarrifId", id).getSingleResult();
    }

    @Override
    public EntityManager getEntityManager() {
        return this.entityManager;
    }

    @Override
    public List<TarrifsModel> getTarrifsByName(String name) {
        return (List<TarrifsModel>) entityManager.createNamedQuery("TarrifsModel.findByName").setParameter("name", name).getResultList();
    }

    @Override
    public List<TarrifsModel> getTarrifsByIssueDate(Date issueDate) {
        return (List<TarrifsModel>) entityManager.createNamedQuery("TarrifsModel.findByIssueDate").setParameter("issueDate", issueDate).getResultList();
    }

    @Override
    public List<TarrifsModel> getTarrifsByEndDate(Date endDate) {
        return (List<TarrifsModel>) entityManager.createNamedQuery("TarrifsModel.findByEndDate").setParameter("endDate", endDate).getResultList();
    }

    @Override
    public List<TarrifsModel> getTarrifsByDurationTime(String durationTime) {
        return (List<TarrifsModel>) entityManager.createNamedQuery("TarrifsModel.findByDurationTime").setParameter("durationTime", durationTime).getResultList();
    }

    @Override
    public List<TarrifsModel> getTarrifsByRank(Double rank) {
        return (List<TarrifsModel>) entityManager.createNamedQuery("TarrifsModel.findByRank").setParameter("rank", rank).getResultList();
    }

    @Override
    public List<TarrifsModel> getTarrifsByCarType(String carType) {
        return (List<TarrifsModel>) entityManager.createNamedQuery("TarrifsModel.findByCarType").setParameter("carType", carType).getResultList();
    }

    @Override
    public int getTarrifsByIssueDateSize(Date issueDate) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(issueDate);

        Calendar mycal = new GregorianCalendar(
                cal.get(Calendar.YEAR),
                cal.get(Calendar.MONTH),
                cal.get(Calendar.DAY_OF_MONTH));
        int max = mycal.getActualMaximum(Calendar.DAY_OF_MONTH);
        int sum = 0;
        cal.set(Calendar.DAY_OF_MONTH, 1);
        for (int i = 0; i < max; i++) {


            List<TarrifsModel> list = (List<TarrifsModel>) entityManager.createNamedQuery("TarrifsModel.findByIssueDate").setParameter("issueDate", cal.getTime()).getResultList();
            sum = sum + list.size();
            cal.add(Calendar.DATE, 1);
        }

        return sum;
    }

    @Override
    public List<TarrifsModel> getTarrifsByCarTypeAndDT(String carType, String time) {
          return (List<TarrifsModel>) entityManager.createNamedQuery("TarrifsModel.findByCarTypeAndDT").setParameter("carType", carType).setParameter("time", time).getResultList();
    }
}
